package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderMobileController {

    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map){
        Jedis jedis = jedisPool.getResource();
        //手机号码
        String telephone=map.get("telephone");
        //验证码的key
        String validateCode= RedisMessageConstant.SENDTYPE_ORDER+"_"+telephone;
        //Redis的验证码
        String codeInRedis =jedis.get(validateCode);
        //判断Redis里面是否为空
        if (StringUtils.isEmpty(codeInRedis)){
            //为空 重新发送
            return new Result(false,"请重新获取验证码");
        }
        //判断验证码是否跟Redis中的验证码一致
        if (codeInRedis.equals(validateCode)){
            //不一致 重新输入
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
        Order order = orderService.ordersubmit(map);
        return new Result(true, MessageConstant.ORDER_SUCCESS,order);
    }


    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        Map<String,Object> map = orderService.findById4Detail(id);
        return new Result(true,MessageConstant.ORDER_SUCCESS,map);
    }

}
